//
//  LLComposeToolbar.swift
//  xinlangweibo
//
//  Created by 任玉林 on 16/7/3.
//  Copyright © 2016年 任玉林. All rights reserved.
//

import UIKit

// toolbar 按钮枚举
enum LLComposeToolbarType: Int{
    // 图片
    case Picture = 10
    // @
    case Mention = 20
    // #
    case Trend = 30
    // 表情
    case Emoticon = 40
    // +
    case Add = 50
}

class LLComposeToolbar: UIView {
    
    var emoticonButton: UIButton?
    
    var isEmoticon: Bool = false {
    
    didSet{
        
        if isEmoticon {
            
            emoticonButton?.setImage(UIImage(named: "compose_keyboardbutton_background"), forState: UIControlState.Normal)
            emoticonButton?.setImage(UIImage(named: "compose_keyboardbutton_background_highlighted"), forState: UIControlState.Highlighted)
        }else{
            
            emoticonButton?.setImage(UIImage(named: "compose_emoticonbutton_background"), forState: UIControlState.Normal)
            emoticonButton?.setImage(UIImage(named: "compose_emoticonbutton_background_highlighted"), forState: UIControlState.Highlighted)
        }
    
    }
}
    
    //定义闭包
    var clouser:((Type:LLComposeToolbarType)->())?

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置视图
    func setupUI(){
        
        backgroundColor = UIColor.whiteColor()
        
      let pictureButton = creatButton("compose_toolbar_picture", Type: .Picture)
     let mentionButton = creatButton("compose_mentionbutton_background", Type: .Mention)
     let trendButton =  creatButton("compose_trendbutton_background", Type: .Trend)
     emoticonButton =  creatButton("compose_emoticonbutton_background", Type: .Emoticon)
     let addButton =  creatButton("compose_add_background", Type: .Add)
        
        pictureButton.snp_makeConstraints { (make) -> Void in
            
            make.top.leading.equalTo(self)
            
            make.width.equalTo(mentionButton)
            
        }
        mentionButton.snp_makeConstraints { (make) -> Void in
            
            make.top.bottom.equalTo(self)
            
            make.width.equalTo(trendButton)
            
            make.leading.equalTo(pictureButton.snp_trailing)
        }
        
        trendButton.snp_makeConstraints { (make) -> Void in
            
            make.top.bottom.equalTo(self)
            
            make.width.equalTo(emoticonButton!)
            
            make.leading.equalTo(mentionButton.snp_trailing)
        }
        emoticonButton!.snp_makeConstraints { (make) -> Void in
            
            make.top.bottom.equalTo(self)
            
            make.width.equalTo(addButton)
            
            make.leading.equalTo(trendButton.snp_trailing)
        }
        addButton.snp_makeConstraints { (make) -> Void in
            
            make.top.bottom.trailing.equalTo(self)
            
            make.leading.equalTo(emoticonButton!.snp_trailing)
        }
    }
    
    //点击方法
    @objc func buttonClick(button: UIButton){
        
        clouser?(Type:LLComposeToolbarType(rawValue: button.tag)!)
    }
    
 //创建按钮compose_mentionbutton_background_highlighted
    func creatButton(imageName: String,Type: LLComposeToolbarType) -> UIButton{
        
        let button = UIButton()
        
        //将枚举 转换为 int
        button.tag = Type.rawValue
        
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Highlighted)
        
        button.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        
        button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: UIControlState.Highlighted)
        
        addSubview(button)
        
        button.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }

}
